package top.wardyan.wdev.dbdevassist.action.base;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import top.wardyan.base.model.BaseModel;
import top.wardyan.conf.Const;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WTable;
import top.wardyan.control.WTableItem;
import top.wardyan.control.WText;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.config.DDA;

public class DicSelectDialog extends Dialog {

	protected int result = 0;
	protected Shell shell;
	
	WTable tblDic;
	
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public DicSelectDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public int open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		//result = text.getText();
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.TITLE | SWT.CLOSE);
		shell.setSize(800, 600);
		shell.setText("选择字典");
		shell.setLayout(new FormLayout());
		shell.setBackground(Const.COLOR_WHITE);
		
		WComposite comTop = new WComposite(shell, SWT.NONE);	
		comTop.setLayout(new FormLayout());
		FormData fd_comTop = new FormData();
		fd_comTop.left = new FormAttachment(shell, 0);
		fd_comTop.top = new FormAttachment(shell, 2);
		fd_comTop.right = new FormAttachment(100,-2);
		fd_comTop.bottom = new FormAttachment(comTop, 35);
		comTop.setLayoutData(fd_comTop);
		
		WText txtKeyword = new WText(comTop, SWT.BORDER);
		txtKeyword.setText("");
		txtKeyword.setTextLimit(32);
		txtKeyword.setToolTipText("关键字");
		FormData fd_txtKeyword = new FormData();
		fd_txtKeyword.left = new FormAttachment(comTop, 4);
		fd_txtKeyword.top = new FormAttachment(comTop, 2);
		fd_txtKeyword.right = new FormAttachment(txtKeyword, 200);
		fd_txtKeyword.bottom = new FormAttachment(txtKeyword, 30);
		txtKeyword.setLayoutData(fd_txtKeyword);
		txtKeyword.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				tblDic.show("tblDic", txtKeyword.getText(), DDA.getInstance().dicService.List());
			}
		});
		
		
		WButton btnRefresh = new WButton(comTop, SWT.PUSH);
		btnRefresh.setText("刷新");
		FormData fd_btnRefresh = new FormData();
		fd_btnRefresh.left = new FormAttachment(txtKeyword, 4);
		fd_btnRefresh.top = new FormAttachment(comTop, 2);
		fd_btnRefresh.right = new FormAttachment(btnRefresh, 64);
		fd_btnRefresh.bottom = new FormAttachment(btnRefresh, 30);
		btnRefresh.setLayoutData(fd_btnRefresh);
		btnRefresh.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				tblDic.show("tblDic", txtKeyword.getText(), DDA.getInstance().dicService.List());
			}
			
		});
		
		WButton btnOK = new WButton(comTop, SWT.PUSH);
		btnOK.setText("确定");
		FormData fd_btnOK = new FormData();
		fd_btnOK.left = new FormAttachment(btnRefresh, 4);
		fd_btnOK.top = new FormAttachment(comTop, 2);
		fd_btnOK.right = new FormAttachment(btnOK, 64);
		fd_btnOK.bottom = new FormAttachment(btnOK, 30);
		btnOK.setLayoutData(fd_btnOK);
		btnOK.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				select();
			}
			
		});
		
		WComposite comTable = new WComposite(shell, SWT.NONE);
		comTable.setLayout(new FormLayout());
		FormData fd_comTable = new FormData();
		fd_comTable.left = new FormAttachment(shell, 4);
		fd_comTable.top = new FormAttachment(comTop, 5);
		fd_comTable.right = new FormAttachment(100, -5);
		fd_comTable.bottom = new FormAttachment(100);
		comTable.setLayoutData(fd_comTable);
				
		tblDic = new WTable(comTable, SWT.NONE, "tblDic", DDA.getInstance().dicService);
		FormData fd_tblDic = new FormData();
		fd_tblDic.left = new FormAttachment(comTable, 0);
		fd_tblDic.top = new FormAttachment(comTable, 0);
		fd_tblDic.right = new FormAttachment(100, -5);
		fd_tblDic.bottom = new FormAttachment(100);
		tblDic.setLayoutData(fd_tblDic);
		
		tblDic.show("tblDic", txtKeyword.getText(), DDA.getInstance().dicService.List());

		ShellUtil.center(shell);
		
//		tblDic.addSelectionListener(new SelectionAdapter() {
//
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				select();
//			}
//			
//		});
		tblDic.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				select();
			}
			
		});
	}
	
	private void select() {
		int index = tblDic.getSelectionIndex();
		if(index >= 0) {
			WTableItem item = (WTableItem)tblDic.getItem(index);
			BaseModel<?> m = item.getModel();
			if(m != null) {
				result = m.getInt("sID");
				shell.dispose();
			}
		}
	}
	
	
	public static void main(String[] args) {
		//new DicSelectDialog()).open();
	}

}
